package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //登录方法
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee,HttpServletRequest request) {
        //1.将页面提交的密码进行md5加密处理,得到加密后的字符串
        String password = employee.getPassword();
        String md5PassWord = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据页面提交的用户名查询数据库中员工数据信息
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        //3.如果没有查询到结果,返回登录失败结果
        if (emp == null) {
            return R.error("登录失败");
        }
        //4.如果查询到用户数据,进行密码比对,不一致则返回登陆失败结果
        String passwordFromDb = emp.getPassword();
        if (!passwordFromDb.equals(md5PassWord)) {
            return R.error("登录失败");
        }
        //5.密码比对成功,查看用户状态,状态为禁用,则返回员工已被禁用的结果
        Integer statusFromDb = emp.getStatus();
        if (statusFromDb == 0) {
            return R.error("账号已经被禁用");
        }
        //6.用户状态为启用,员工登录成功,将员工id存入Session,并返回登录成功的结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    //退出方法
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //清理Session中保存的当前登录员工id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    //添加员工
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request) {
        log.info("新增员工,员工信息为:{}",employee.toString());
        String str = "123456";
        //对密码进行加密
        String md5Password = DigestUtils.md5DigestAsHex(str.getBytes());
        employee.setPassword(md5Password);
        //获取登陆者id
//        Long loginId = (Long) request.getSession().getAttribute("employee");
        //设置创建时间,创建人,修改时间,修改人(该信息从Session中获取)
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setCreateUser(loginId);
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(loginId);
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name) {
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
        //分页构造器
        Page p = new Page();
        //条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        //模糊查询
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件(时间倒序)
        lqw.orderByDesc(Employee::getCreateTime);
        //执行查询
        employeeService.page(p,lqw);
        //返回结果
        return R.success(p);
    }

    //员工信息更新
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee) {
        //打印修改员工信息
        log.info(employee.toString());
        //获取用户ID
//        Long userid = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(userid);
//        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
//        long threadId = Thread.currentThread().getId();
//        log.info("updateController线程id:"+threadId);
        return R.success("员工信息修改成功");
    }

    //根据id查询
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("没有查询到相关员工信息");
    }
}
